package com.a3dweijing.wetouch2.Activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;


import com.a3dweijing.wetouch2.R;

import static android.view.KeyEvent.KEYCODE_BACK;

/**
 * Created by yxd on 2017/12/2.
 */

public class ExpoonWebViewActivity extends AppCompatActivity {
    private WebView webView;
    private TextView loading;
    private String uri = "http://www.expoon.com/11369/?from=timeline&isappinstalled=0";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        String s=getIntent().getStringExtra("url");
        if (s!=null && s.length()>0){
            uri=s;
        }
        setContentView(R.layout.activity_expoon);
        loading = (TextView) findViewById(R.id.webiew_txt);
        webView = (WebView) findViewById(R.id.webiew_web);
//        支持javaScript
        webView.getSettings().setJavaScriptEnabled(true);
//        是否支持viewport
        webView.getSettings().setUseWideViewPort(true);

        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setUseWideViewPort(true);
//        是否适配webview的宽度
        webView.getSettings().setLoadWithOverviewMode(true);
//        设置支持的字符集
        webView.getSettings().setDefaultTextEncodingName("UTF-8");

        webView.getSettings().setDomStorageEnabled(true);
        //1：设置webview地址
        webView.loadUrl(uri);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
//设置WebChromeClient类
        webView.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {

            }
            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    String progress = newProgress + "%";
                    Log.e("--", progress);
                } else if (newProgress == 100) {
                    String progress = newProgress + "%";
                    Log.e("--", progress);
                }
            }
        });


        //设置WebViewClient类
        webView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                Log.e("--", "开始加载了");

            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.e("--", "结束加载了");
                loading.setVisibility(View.GONE);
            }
        });

    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();

            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }
}
